package net.xdclass.online_xdclass.config;

import net.xdclass.online_xdclass.intercepter.CorsIntercepter;
import net.xdclass.online_xdclass.intercepter.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器配置类
 * 不用权限可以访问url /api/v1/pub/
 * 要登录可以访问url /api/v1/pri/
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    LoginInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }

    @Bean
    CorsIntercepter corsIntercepter(){
        return new CorsIntercepter();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("InterceptorRegistry========");
        //拦截全部路径，这个跨域了，需要放在最上面
        registry.addInterceptor(corsIntercepter()).addPathPatterns("/**");

        registry.addInterceptor(loginInterceptor()).addPathPatterns("/api/v1/pri/*/*/**")
                //不拦截哪些路径 斜杠一定要加
                .excludePathPatterns("/api/v1/pri/user/login","/api/v1/pri/user/register");
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
